package cn.edu.scut.suggestion.corpus;

/**
 *对本地文件语料库进行写入
 *@author tian.yuchen
 */
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class FileDBWriter {
	DataOutputStream out;
	TermInfo[] tInfos;
	
	public FileDBWriter(File file,TermInfo[] tInfos) {
		try {
			out=new DataOutputStream(new FileOutputStream(file));
			this.tInfos=tInfos;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public TermInfo[] write() {
		try {
			long position=0;
			for (int i=0;i<tInfos.length;i++) {
				tInfos[i].pos=(long) out.size();
				for (int docpos : tInfos[i].docs) {
					out.writeInt(docpos);
				}
			}
		
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tInfos;	
	}
	
	public void dump(){
		for (int i=0;i<tInfos.length;i++){
			System.out.println(tInfos[i].term.text()+" "+tInfos[i].pos);
		}
	}
}
